﻿using System;
using System.Web;
using SP2010.Enhancements.Management;

namespace SP2010.Enhancements.SEO
{ 
    internal class SEOHttpHandler : IHttpModule
    {
        private void addFilter(object sender, EventArgs e)
        {
            HttpApplication application = null;
            application = (HttpApplication) sender;
            if (((application != null) && !this.isAuthenticated(application.Request.Cookies.AllKeys)) && application.Request.RawUrl.Contains(".aspx"))
            {
                application.Response.Filter = new SEOFilter(application.Response.Filter);
            }
        }
        public void Dispose()
        {
        }
        public void Init(HttpApplication httpApplication)
        {
            try
            {
                httpApplication.BeginRequest += new EventHandler(this.addFilter);
            }
            catch (Exception exception)
            {
                new Tools().logErrors(SEOConstants.ERROR_TRACING_CATEGORY_SEO, exception);
            }
        }
        private bool isAuthenticated(string[] cookies)
        {
            int length = cookies.Length;
            for (int i = 0; i < length; i++)
            {
                if (cookies.GetValue(i).ToString() == "WSS_KeepSessionAuthenticated")
                {
                    return true;
                }
            }
            return false;
        }
    }
}

